SoftMutuallyBoundMinMaxRule 2.cs :  » GUI » Paint.net » PaintDotNet » PropertySystem » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » GUI » Paint.net 
Paint.net » PaintDotNet » PropertySystem » SoftMutuallyBoundMinMaxRule`2.cs
/////////////////////////////////////////////////////////////////////////////////
// Paint.NET                                                                   //
// Copyright (C) Rick Brewster, Tom Jackson, and past contributors.            //
// Portions Copyright (C) Microsoft Corporation. All Rights Reserved.          //
// See src/Resources/Files/License.txt for full licensing and attribution      //
// details.                                                                    //
// .                                                                           //
/////////////////////////////////////////////////////////////////////////////////

using System;

namespace PaintDotNet.PropertySystem{
    /// <summary>
    /// Defines a rule for two properties, A and B, such that A will always be less than or equal to B,
    /// and B will always be greater than or equal to A. If A is set to a new value that is greater than B,
    /// then B will be set equal to A. If B is set to a new value that is less than A, then A will be set
    /// equal to B.
    /// </summary>
    public sealed class SoftMutuallyBoundMinMaxRule<TValue, TProperty>
        : PropertyCollectionRule
          where TProperty : ScalarProperty<TValue>
          where TValue : struct, IComparable<TValue>
    {
        private string minPropertyName;
        private string maxPropertyName;

        public SoftMutuallyBoundMinMaxRule(Property minProperty, Property maxProperty)
            : this(minProperty.Name, maxProperty.Name)
        {
        }

        public SoftMutuallyBoundMinMaxRule(object minPropertyName, object maxPropertyName)
        {
            this.minPropertyName = minPropertyName.ToString();
            this.maxPropertyName = maxPropertyName.ToString();
        }

        protected override void OnInitialized()
        {
            TProperty minProperty = (TProperty)Owner[this.minPropertyName];
            TProperty maxProperty = (TProperty)Owner[this.maxPropertyName];

            if (ScalarProperty<TValue>.IsGreaterThan(minProperty.MinValue, maxProperty.MinValue))
            {
                throw new ArgumentOutOfRangeException("MinProperty.MinValue must be less than or equal to MaxProperty.MinValue");
            }

            if (ScalarProperty<TValue>.IsGreaterThan(minProperty.MaxValue, maxProperty.MaxValue))
            {
                throw new ArgumentOutOfRangeException("MinProperty.MaxValue must be less than or equal to MaxProperty.MaxValue");
            }

            // Analyze the PropertyCollection we are bound to in order to ensure that we do not
            // have any "infinite loops". It is safe to simply ensure that no other SoftMutuallyBoundMinMaxRule
            // has minPropertyName as a maxPropertyName.
            foreach (PropertyCollectionRule rule in this.Owner.Rules)
            {
                SoftMutuallyBoundMinMaxRule<TValue, TProperty> asOurRule = rule as SoftMutuallyBoundMinMaxRule<TValue, TProperty>;

                if (asOurRule != null)
                {
                    if (asOurRule.maxPropertyName.ToString() == this.minPropertyName.ToString())
                    {
                        throw new ArgumentException("The graph of SoftMutuallyBoundMinMaxRule's in the PropertyCollection has a cycle in it");
                    }
                }
            }

            minProperty.ValueChanged += new EventHandler(MinProperty_ValueChanged);
            maxProperty.ValueChanged += new EventHandler(MaxProperty_ValueChanged);
        }

        private void MaxProperty_ValueChanged(object sender, EventArgs e)
        {
            TProperty minProperty = (TProperty)Owner[this.minPropertyName];
            TProperty maxProperty = (TProperty)Owner[this.maxPropertyName];

            if (maxProperty.IsLessThan(minProperty))
            {
                minProperty.Value = maxProperty.Value;
            }
        }

        private void MinProperty_ValueChanged(object sender, EventArgs e)
        {
            TProperty minProperty = (TProperty)Owner[this.minPropertyName];
            TProperty maxProperty = (TProperty)Owner[this.maxPropertyName];

            if (minProperty.IsGreaterThan(maxProperty))
            {
                maxProperty.Value = minProperty.Value;
            }
        }

        public override PropertyCollectionRule Clone()
        {
            return new SoftMutuallyBoundMinMaxRule<TValue, TProperty>(this.minPropertyName, this.maxPropertyName);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.